package cn.dtsmusic.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.chips.rong.RongCloud;
import com.chips.rong.model.Result;
import com.chips.rong.models.response.TokenResult;
import com.chips.rong.models.user.UserModel;

import cn.dtsmusic.chips.test.Constant;
import cn.dtsmusic.model.domain.RequestUserParam;
import cn.dtsmusic.model.domain.ResponseResult;
import cn.dtsmusic.model.domain.TestUserInfo;
import cn.dtsmusic.model.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path = "/user")
@Api(value = "UserController|一个用户接口相关的控制器")
public class UserController {

	private static Log log = LogFactory.getLog(UserController.class );
	
	@GetMapping("/getUserFriends/{id}")
    public ResponseResult getUserFriends(@PathVariable Integer id) {
        
		List<TestUserInfo> friends = new ArrayList<>();
		for(int i=0;i<20;i++){
			TestUserInfo uinfo = new TestUserInfo();
			
			uinfo.setUserId(i+"");
			uinfo.setUserName("friend"+i);
			uinfo.setAvator("avator"+i);
			friends.add(uinfo);
		}
		
		ResponseResult  rr = new ResponseResult(true,200,"friends",friends,null);
		System.out.println(" /getUserFriends/{id} "+rr);
        return rr;
    }
	
	
	@PostMapping("/sendCode")
	@ApiOperation(value="向手机发送验证码", notes="正常返回,返回的 success为 true")
    public ResponseResult sendCode(@RequestBody RequestUserParam param) {
        
		
		RongCloud rongCloud = RongCloud.getInstance(Constant.APPKEY, Constant.APPSECRET);
    	
    	com.chips.rong.methods.user.User user2 = rongCloud.user;
    	
    	log.debug("come in sendCode  ");
    	
		ResponseResult  rr = new ResponseResult(true,200,"send_code 请求信息已经收到");
        return rr;
    }
	
	@PostMapping("/updateUser")
	@ApiOperation(value="更新用户的", notes="正常返回,返回的 success为 true")
    public ResponseResult updateUser(@RequestBody RequestUserParam param) {
        
		
		RongCloud rongCloud = RongCloud.getInstance(Constant.APPKEY, Constant.APPSECRET);
    	
    	com.chips.rong.methods.user.User user2 = rongCloud.user;
    	
    	
    	
    	UserModel user = new UserModel()
                .setId("userxxd1")
                .setName("chipsUser1-up")
                .setPortrait("http://www.rongcloud.cn/images/logo.png");
		 
		 //通过 id  ， 更新用户的 name 和 Portrait 地址信息
//		 Result update = user2.update(user);
		 
		/* // 成功后更新数据库
		 if(update.getCode() == 200){
			 
			 User users = new User();
			 users.setUserId("userxxd1");
			 
			 User selectOne = userService.selectOne(users);
			 if(selectOne != null){
				 selectOne.setNickName("chipsUser1-up");
				 selectOne.setPortraitUri("http://www.rongcloud.cn/images/logo.png");
				 
				 userService.update(selectOne);
			 }
		 }*/
		 
    	
    	
    	
    	log.debug("come in sendCode  ");
    	
		ResponseResult  rr = new ResponseResult(true,200,"send_code 请求信息已经收到");
        return rr;
    }
	
	
	@PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        
		
		RongCloud rongCloud = RongCloud.getInstance(Constant.APPKEY, Constant.APPSECRET);
    	
    	com.chips.rong.methods.user.User user2 = rongCloud.user;
    	
    	log.debug("come in register users ");
    	
    	if(user != null && user.getUserId() != null && user.getNickName() != null && user.getPortraitUri() != null){
    		UserModel rongyunUser = new UserModel()
                    .setId(user.getUserId())
                    .setName(user.getNickName())
                    .setPortrait(user.getPortraitUri());
            String message = "";
            ResponseResult  rr = null ;
            try {
				TokenResult result = user2.register(rongyunUser);
				if(result != null){
					message = result.getToken();
				}
				 rr = new ResponseResult(true,200,message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				rr = new ResponseResult(false,-1,e.getMessage());
			}
            return rr;
    	}
		ResponseResult  rr = new ResponseResult(false,-1,"用户信息不完整");
        return rr;
    }
	
	
	
//	@PostMapping("/postUserFriends/{id}")
	@RequestMapping(method = RequestMethod.POST,value="/postUserFriends")
    public ResponseResult postUserFriends() {
        
		List<TestUserInfo> friends = new ArrayList<>();
		for(int i=0;i<20;i++){
			TestUserInfo uinfo = new TestUserInfo();
			
			uinfo.setUserId(i+"");
			uinfo.setUserName("friend"+i);
			uinfo.setAvator("avator"+i);
			friends.add(uinfo);
		}
		
		ResponseResult  rr = new ResponseResult(true,200,"friends",friends,null);
		System.out.println(" /postUserFriends "+rr);
        return rr;
    }
}
